package com.etc.controller;
import java.io.File;
/**
 * 后台登录页面的控制层
 * author:GMY
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import com.etc.entity.Admin;
import com.etc.service.AdminLoginService;
import com.etc.service.impl.AdminLoginServiceImpl;


/**
 * Servlet implementation class AdminLoginServlet
 */
@WebServlet("/AdminLogin")
@MultipartConfig 
public class AdminLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AdminLoginService adminLoginService = new AdminLoginServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminLoginServlet() {
        super(); 
        // TODO Auto-generated constructor stub
    }
 
	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if ("admin_logout".equals(op)) {
			// 退出登录的请求
			// 1.从会话中移除指定的属性
			HttpSession session = request.getSession();
			session.removeAttribute("ADMIN_LOGIN");
			// 2.重定向登录页面
			response.sendRedirect(request.getContextPath() + "/back/login.jsp");
		}else if ("admin_login".equals(op)){
			dologin(request,response);
		}
		
		
		
	}

	private void dologin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String mobile = request.getParameter("mobile");
		String password = request.getParameter("password");
		String rememberme = request.getParameter("rememberme");
		
		Admin admin = adminLoginService.login(mobile, password);
		if (admin != null) {
			// 登录成功
			// 存储在会话中
			HttpSession session = request.getSession();
			// 判断角色
			if (admin instanceof Admin) {
				session.setAttribute("ADMIN_LOGIN", (Admin) admin);
			}
			// 判断是否选择记住密码,记住则将用户名和密码存储在cookie
			if ("rememberme".equals(rememberme)) {
				// 单用户：name设置”rememberMe”  value设置登录账号+密码
				// 1.创建cookie
				Cookie cookie = new Cookie("rememberMe", mobile+"="+password);
				// 2.设置过期时间
				cookie.setMaxAge(60*60*24); // 24小时
				// 3.将cookie添加到响应头中
				response.addCookie(cookie);
				
			}else {
				// 1.创建cookie
				Cookie cookie = new Cookie("rememberMe", mobile+"="+password);
				// 2.设置过期时间
				cookie.setMaxAge(0); // 24小时
				// 3.将cookie添加到响应头中
				response.addCookie(cookie);
			}
			// 重定向到首页
			response.sendRedirect(request.getContextPath() + "/back/index.jsp");
		} else {
			// 登录失败,重定向回登录页面
			response.sendRedirect(request.getContextPath() + "/back/login.jsp?message=fail");
		}
		
	}
}
